﻿var pageIndex = 1;
var pageSize = 20;
var page_count = 0;

$.ajaxSetup({ url: "Proxy.ashx", type: "post", dataType: "json" });

// 绑定部门列表
function BindDepartment() {
    $.ajax({
        data: { op: "ListDept" }
        , success: function (json) {
            $("#ddl_dept").append("#UserList_Dept", json.ListDept);
            DepartmentSelected();
            btnSearch_Click();
        }
    });
}

// 部门列表选择时绑定岗位列表
function DepartmentSelected() {
    var dept_id = $("#ddl_dept ").val();
    $.ajax({
        async: false,
        data: { op: "ListJobByDept", dept_id: dept_id }
        , success: function (json) {            
            if ($("#ddl_job option").length > 0) {
                $("#ddl_job").empty();
            }
            $("#ddl_job").append("#UserList_Job", json.ListJobByDept);
        }
    });
}

function btnSearch_Click() {
    var deptId = $("#ddl_dept").val(); // 所属机构
    var jobId = $("#ddl_job").val(); //岗位    
    var loginName = $("#txtLoginName").val(); // 登录名
    var userID = $("#txtUserID").val(); // 登录名
    var username = $("#txtUsername").val(); // 用户名

    $.ajax({
        data: { op: "ListUser",
            deptId: deptId,
            jobId: jobId,
            userId: userID,
            loginName: loginName,
            username: username,
            page: pageIndex,
            size: pageSize
        }
        , success: function (json) {

            // 先删除原来的行，否则会多次绑定。
            $("#tbl_userList tr").each(function (trindex, tritem) {
                if (trindex != 0)
                    $(tritem).remove();
            });
            // 填充表格
            $("#tbl_userList").append("#full_userList", json.ListUser);

            // 分页
            page_count = Math.ceil(json.RowsCount / pageSize);

            PagerInit();

        }
    });

}

function DeleteUser(id) {
    $.ajax({
        data: { op: "DelUser",
            userId: id
        }
        , success: function (json) {
            var isSuccess = json.DelUser[0].status;
            if (isSuccess == "success") {
                alert("用户删除成功！")
            }
            else {
                alert("用户删除失败！详细信息：\r\n" + json.DelUser[0].falseReason);
            }
        }
    });
}

function PageClick(pageclickednumber) {
    $("#pager").pager({ pagenumber: pageclickednumber, pagecount: page_count, buttonClickCallback: PageClick });
    pageIndex = pageclickednumber;
}

function PagerInit() {
    $(document).ready(function () {
        $("#pager").pager({ pagenumber: pageIndex, pagecount: page_count, buttonClickCallback: PageClick });
    });
}

function init() {
    BindDepartment();

    $("#btnSearch").click(function () {
        btnSearch_Click();
    });


}
